/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CSMLINK2_DEFINE_H
#define __CSMLINK2_DEFINE_H

/**
 * IO definitions
 *
 * define access restrictions to peripheral registers
 */
#define     __I     volatile const   /*!< defines 'read only' permissions     */
#define     __O     volatile         /*!< defines 'write only' permissions    */
#define     __IO    volatile         /*!< defines 'read / write' permissions  */

/*!< Signed integer types  */
typedef signed char     int8_t;
typedef signed short    int16_t;
typedef signed long     int32_t;

/*!< Unsigned integer types  */
typedef unsigned char     uint8_t;
typedef unsigned short    uint16_t;
typedef unsigned long     uint32_t;

/*!< STM8Lx Standard Peripheral Library old types (maintained for legacy purpose) */

typedef int32_t  s32;
typedef int16_t s16;
typedef int8_t  s8;

typedef uint32_t  u32;
typedef uint16_t u16;
typedef uint8_t  u8;


typedef enum {FALSE = 0, TRUE = !FALSE} bool;

typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus, BitStatus, BitAction;

typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
#define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE))

typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;

#define U8_MAX     (255)
#define S8_MAX     (127)
#define S8_MIN     (-128)
#define U16_MAX    (65535u)
#define S16_MAX    (32767)
#define S16_MIN    (-32768)
#define U32_MAX    (4294967295uL)
#define S32_MAX    (2147483647)
#define S32_MIN    (-2147483648uL)

/*CSM Communication Portocol*/

/*2 Physical Layer*/
/*
Asynchronous serial encoded as: 
?   1 Start bit 
?   8 Data bits 
?   1 Stop bit 
?   No parity bit 
The point-to-point connection is RS232 serial interface at: 
?   Baud rate:   115200 (fixed) 
?  Handshake:  RTS/CTS
*/

/*3 Data Link Layer*/
/*
3.1 Frame structure
---------------------------------------------------
Bytes    Description        Data        See Section
---------------------------------------------------
1        SOM                0xFF
---------------------------------------------------
1        COMMAND TYPE                   4.1
---------------------------------------------------
1        LENGTH             Length n
---------------------------------------------------
N        DATA                           4
---------------------------------------------------
2        CRC                            5
---------------------------------------------------
1        EOM                0xFE        
---------------------------------------------------
*/

/*4 Application Layer*/
/*
4.1 Command Type
CSM on-line data 4.2
Command Type = 1
*/

/*
4.2 Command Type 1: CSM on-line data is specified as:
-----------------------------------------------------------------------------
Byte    Name                Range            Description
-----------------------------------------------------------------------------
4       CSM serial No.      2004210000:        
                            2099219999
-----------------------------------------------------------------------------
1       Protocol version    1:255
-----------------------------------------------------------------------------
1       CSI version         1:255
-----------------------------------------------------------------------------
2       Device time         0:65535          Total time from start in seconds
-----------------------------------------------------------------------------
1       Block Status        Bit field        Bit 0: Artefact
                                             Bit 1: Electrode alarm
                                             Bit 2: SQI low
                                             Bit 3: Impedance high
-----------------------------------------------------------------------------
1       Event number        0:255
-----------------------------------------------------------------------------
1       Event type          0:8              0 = General event
                                             1 = Induction
                                             2 = Intubation
                                             3 = Maintenance
                                             4 = Surgery
                                             5 = Injection
                                             6 = Note
                                             7 = En maintenance
                                             8 = Movement
-----------------------------------------------------------------------------
1       CSI                 0:100, 255       255 = Not defined
-----------------------------------------------------------------------------
1       BS%                 0:100, 255       255 = Not defined
-----------------------------------------------------------------------------
1       SQI%                0:100
-----------------------------------------------------------------------------
1       Black Imp           0:11             0 = "<1" 11 = ">10"
-----------------------------------------------------------------------------
1       White Imp           0:11             0 = "<1" 11 = ">10"
-----------------------------------------------------------------------------
1       EMG (Bar)           0:100, 255       255 = Not defined
-----------------------------------------------------------------------------
1       Battery Voltage     0:255            20 * Voltage
-----------------------------------------------------------------------------
1       Reserved            0:255
-----------------------------------------------------------------------------
1       Alarm high          0:255            Alarm limit. Bit 7: Alarm on/off
-----------------------------------------------------------------------------
1       Alarm low           0:255            Alarm limit. Bit 7: Alarm on/off
-----------------------------------------------------------------------------
4       Reserved
-----------------------------------------------------------------------------
100     EEG                 -128:127         Binary EEG data. Signed byte.
                                             - 180 to +180 uV signal range
-----------------------------------------------------------------------------

NOTE: On-line data is transmintted as one sequence per second.
*/

/*The structure of communication procotol*/

#define     _CSMLINK2_ONLINE_SOM_           0xFF    /*Online : Start of Message = 0xFF*/
#define     _CSMLINK2_ONLINE_EOM_           0xFE    /*Online : End of Message = 0xFE*/

/*Define the type of event*/
                                                    /*Byte    Name                Range            Description*/
#define     _CSMLINK2_ET_GENERAL_           0x00    /*1       Event type          0:8              0 = General event*/
#define     _CSMLINK2_ET_INDUCTION_         0x01    /*                                             1 = Induction*/
#define     _CSMLINK2_ET_INTUBATION_        0x02    /*                                             2 = Intubation*/
#define     _CSMLINK2_ET_MAINTENANCE_       0x03    /*                                             3 = Maintenance*/
#define     _CSMLINK2_ET_SURGERY_           0x04    /*                                             4 = Surgery*/
#define     _CSMLINK2_ET_INJECTION_         0x05    /*                                             5 = Injection*/
#define     _CSMLINK2_ET_NOTE_              0x06    /*                                             6 = Note*/
#define     _CSMLINK2_ET_ENMAITENANCE_      0x07    /*                                             7 = En maintenance*/
#define     _CSMLINK2_ET_MOVEMENT_          0x08    /*                                             8 = Movement*/

/*Communication Protocol*/
typedef    struct
{
/*TYPE & LENGTH PART*/
                                                    /*Bytes    Description        Data        See Section*/
    u8      u8CommandType;                          /*1        COMMAND TYPE                   4.1*/
    u8      u8CommandLength;                        /*1        LENGTH            Length n, size of the DATA part, assume equals 125.*/
    
/*DATA PART*/
                                                    /*N        DATA                        4*/
                                                    
                                                    /*Byte    Name                Range            Description*/
    u32     u32CSMSerialNum;                        /*4        CSM serial No.     2004210000:2099219999*/
    u8      u8ProtocolVer;                          /*1        Protocol version   1:255*/
    u8      u8CSIVer;                               /*1        CSI version        1:255*/
    u16     u16DeviceTime;                          /*2        Device time        0:65535            Total time from start in seconds*/
    u8      fBlockStatus_Artefact           :   1;  /*1        Block Status       Bit field        Bit 0: Artefact*/
    u8      fBlockStatus_EleAlarm           :   1;  /*                                             Bit 1: Electrode alarm*/
    u8      fBlockStatus_SQILow             :   1;  /*                                             Bit 2: SQI low*/
    u8      fBlockStatus_ImpHigh            :   1;  /*                                             Bit 3: Impedance high*/
    u8      fBlockStatus_Reserved           :   4;
    u8      u8EventNum;                             /*1       Event number        0:255*/
    u8      u8EventType;                            /*1       Event type          0:8              0 = General event
                                                                                                   1 = Induction
                                                                                                   2 = Intubation
                                                                                                   3 = Maintenance
                                                                                                   4 = Surgery
                                                                                                   5 = Injection
                                                                                                   6 = Note
                                                                                                   7 = En maintenance
                                                                                                   8 = Movement*/                                                                                                
    u8      u8CSI;                                  /*1       CSI                 0:100, 255       255 = Not defined*/
    u8      u8BS;                                   /*1       BS%                 0:100, 255       255 = Not defined*/
    u8      u8SQI;                                  /*1       SQI%                0:100*/
    u8      u8BlackImp;                             /*1       Black Imp           0:11             0 = "<1" 11 = ">10"*/
    u8      u8WhiteImp;                             /*1       White Imp           0:11             0 = "<1" 11 = ">10"*/
    u8      u8EMG;                                  /*1       EMG (Bar)           0:100, 255       255 = Not defined*/
    u8      u8BatteryVoltage;                       /*1       Battery Voltage     0:255            20 * Voltage*/
    u8      u8Reserved1;                            /*1       Reserved            0:255*/
    u8      u8AlarmHigh;                            /*1       Alarm high          0:255            Alarm limit. Bit 7: Alarm on/off*/
    u8      u8AlarmLow;                             /*1       Alarm low           0:255            Alarm limit. Bit 7: Alarm on/off*/
    u8      u8Reserved2[4];                         /*4       Reserved*/
    s8      s8EEG[100];                             /*100     EEG                 -128:127         Binary EEG data. Signed byte.
                                                                                                   - 180 to +180 uV signal range*/
/*CRC PART*/    
                                                    /*Bytes    Description        Data        See Section*/
    u16     u16CRC;                                 /*2        CRC                            5*/
}
_tCSMLINK2_ONLINE_DATA_;


#endif    /*__CSMLINK2_DEFINE_H*/